<template>
  <div class="polish-text-container side-nav-content">
    <div class="input">
      <theme-button size="mini" @click="summeryText"
        >开始{{ subTitle }}</theme-button
      >
    </div>
    <span class="title">{{ title }}</span>

    <div class="original">
      <el-input v-model="form.text" type="textarea" class="original-input" />
    </div>
    <div class="title">
      <i class="iconfont icon-xunhuan" />
    </div>
    <div class="code summary">
      <span>{{ translateResult }}</span>
      <i
        v-if="translateResult"
        class="iconfont icon-fuzhi"
        @click="copy(translateResult)"
      />
    </div>

    <div class="right-bottom">
      <!-- <div class="title">全文{{ subTitle }}</div> -->

      <el-upload :action="action" hit="" accept=".docx" :on-success="onSuccess">
        <div class="opr-btn theme-btn-color">上传文献</div>
      </el-upload>
    </div>
  </div>
</template>

<script>
import { fetchData, copy } from "@/utils/common";
import { summeryText } from "@/api/translate";

const defaultForm = {
  text: "",
};
export default {
  name: "Summary",
  props: {
    uploadCallback: {
      type: Function,
      default: new Function(),
    },
    type: {
      type: String,
      default: "1",
    },
    title: {
      type: String,
      default: "",
    },
    subTitle: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      copy,
      form: { ...defaultForm },
      translateResult: "",
    };
  },
  computed: {
    action() {
      return `${process.env.VUE_APP_BASE_URL}/file/upload`;
    },
  },
  methods: {
    validate() {
      if (!this.form.text || !this.form.text.trim()) {
        this.$message.warning("请输入总结内容！");
        return Promise.reject();
      }

      return Promise.resolve();
    },
    summeryText() {
      fetchData(summeryText, { ...this.form, type: this.type }).then((data) => {
        this.translateResult = data?.message;
      });
    },
    onSuccess(res) {
      const url = res?.data?.url;
      url && this.uploadCallback(res?.data?.url);
    },
  },
};
</script>

<style lang="less" scoped>
@import url("./common.less");
.dark-theme .polish-text-container {
  .summary .icon-fuzhi {
    color: #2f7756;
  }
}
.polish-text-container {
  display: flex;
  flex-direction: column;

  .title {
    .icon-xunhuan {
      font-size: 30px;
      cursor: pointer;
    }
  }
  .summary {
    position: relative;
    flex: 18;
    padding-bottom: 30px;
    .icon-fuzhi {
      position: absolute;
      right: 18px;
      bottom: 6px;
      font-size: 27px;
      color: var(--color-primary);
    }
  }
  .code {
    padding: 10px;
    margin-top: 16px;
    border-radius: 2px;
    background-color: var(--translate-code-background);
  }
  .original-input {
    margin-top: 16px;
    /deep/.el-textarea__inner {
      height: 200px;
      background-color: var(--translate-code-background);
    }
  }
  .right-bottom {
    padding-bottom: 46px;
    .opr-btn {
      margin-top: 23px;
      width: 189px;
      height: 54px;
      line-height: 54px;
      text-align: center;
      border-radius: 10px;
    }
  }
}
</style>
